package com.threeox.commonlibrary.util;

import android.app.Activity;

import com.threeox.commonlibrary.eventbus.EventBus;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: ActivityObjs
 * 
 * @Description: TODO(存储程序启动的Activity)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/7/21 15:19
 * 
 * @version 1.0
 * 
 */
public class ActivityObjects {

    private static ActivityObjects mActivityObjects = null;

    private List<Activity> activitys = new ArrayList<Activity>();

    private ActivityObjects(){};

    public static ActivityObjects getInstance() {
        if (mActivityObjects == null) {
            synchronized (EventBus.class) {
                if (mActivityObjects == null) {
                    mActivityObjects = new ActivityObjects();
                    LogUtils.d("初始化EventBus!");
                }
            }
        }
        return mActivityObjects;
    }

    /**
     * 得到所有缓存的Activity
     *
     * @return
     */
    public List<Activity> getActivitys() {
        return activitys;
    }

    /**
     * 得到所有缓存的数量
     *
     * @return
     */
    public int getActivitySize() {
        if (EmptyUtils.isNotEmpty(activitys)) {
            return activitys.size();
        }
        return 0;
    }

    /**
     * 添加一个Activity
     *
     * @param activity
     */
    public void putActivity(Activity activity) {
        activitys.add(activity);
    }

    /**
     * 清除指定Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        activitys.remove(activity);
    }

    /**
     * 清除全部Activity
     */
    public void clearActivity() {
        activitys.clear();
    }

    /**
     * 得到最后的一个Activity
     *
     * @return
     */
    public Activity getLastActivity() {
        if (EmptyUtils.isNotEmpty(activitys)) {
            return activitys.get(activitys.size() - 1);
        }
        return null;
    }

    /**
     * 除去传入Actiivty其他的都关闭
     *
     * @param activity
     * @return
     */
    public void finishActivity(Activity activity) {
        if (EmptyUtils.isNotEmpty(activitys)) {
            for (Activity act: activitys) {
                if (!act.equals(activity)) {
                    act.finish();
                }
            }
        }
    }

    /**
     * 除去传入的Index为位置的Activity其他的都关闭
     *
     * @param index
     */
    public void finishActivity(int index) {
        if (EmptyUtils.isNotEmpty(activitys)) {
            for (int i = 0; i < activitys.size(); i++) {
                if (i != index) {
                    activitys.get(i).finish();
                }
            }
        }
    }

    /**
     * Actiivty其他的都关闭
     *
     * @return
     */
    public void finishActivity() {
        if (EmptyUtils.isNotEmpty(activitys)) {
            for (Activity activity: activitys) {
                activity.finish();
            }
        }
    }

}
